-- FormFocusM
-- create by zouyb
-- 窗口焦点管理

module("FormFocusM", package.seeall);

-- 刚失去焦点的窗口
local formLoseFocus = "";
local formLoseFocusDialog;

-- 目前获得焦点的窗口
local formGetFocus = "";
local formGetFocusDialog;
-- 新增和关闭列表中的界面，使得其他界面获得或者失去焦点，那么其他界面不需要播放特效
local withOutFocusEffectForms = {"UICommunicating", "UILoadingEffect", "UIInvisibleForm", "UIGuideMask"};

-- 记录下焦点
local focusList = {};
local schedulerId;

-- 累计无操作时间
local userIdleTime = 0;

function init()
    EventMgr.removeAll("FormFocusM");

    -- 注册窗口打开的事件
    EventMgr.register("FormFocusM", event.ADD_FORM, function(params)
        if UIMgr.getCurrentScene() == nil or UIMgr.getCurrentScene().getCurrentDialog == nil
           or UIMgr.getCurrentScene():getCurrentDialog() ~= nil then
            -- 还有模态窗口在，不做处理
            return;
        end

        local prevForm = UIMgr.getCurrentScene():getPrevForm();
        if prevForm then
            formLoseFocus = prevForm:getName();
            formLoseFocusDialog = prevForm;
        end

        local currentForm = UIMgr.getCurrentScene():getCurrentForm();
        if currentForm then
            formGetFocus = currentForm:getName();
            formGetFocusDialog = currentForm;
        end

        fireFocusChangeEvent();

        -- 播放界面失去焦点的效果
        if table.indexOf(withOutFocusEffectForms, formGetFocus) <= 0 and
            table.indexOf(withOutFocusEffectForms, formLoseFocus) <= 0 then
            if formLoseFocusDialog then
                FormEffectM.runLoseFocusEffect(formLoseFocusDialog);
            end
        end
    end);

    -- 注册窗口关闭的事件
    EventMgr.register("FormFocusM", event.REMOVE_FORM, function(params)
        if UIMgr.getCurrentScene() == nil
          or UIMgr.getCurrentScene():getCurrentDialog() ~= nil then
            -- 还有模态窗口在，不做处理
            return;
        end

        if UIMgr.getCurrentScene():getCurrentForm() ~= nil then
            formLoseFocus = formGetFocus;
            formLoseFocusDialog = formGetFocusDialog;
            formGetFocus = UIMgr.getCurrentScene():getCurrentForm():getName();
            formGetFocusDialog = UIMgr.getCurrentScene():getCurrentForm();

            fireFocusChangeEvent();

            -- 播放界面获得焦点的效果
            if table.indexOf(withOutFocusEffectForms, formGetFocus) <= 0 and
                table.indexOf(withOutFocusEffectForms, formLoseFocus) <= 0 then
                if formGetFocusDialog then
                    FormEffectM.runGetFocusEffect(formGetFocusDialog);
                end
            end
        end
    end);

    -- 注册添加模态框的事件
    EventMgr.register("FormFocusM", event.ADD_DIALOG, function(params)
        if UIMgr.getCurrentScene() == nil then
            return;
        end

        formLoseFocus = formGetFocus;
        formLoseFocusDialog = formGetFocusDialog;
        formGetFocus = UIMgr.getCurrentScene():getCurrentDialog():getName();
        formGetFocusDialog = UIMgr.getCurrentScene():getCurrentDialog();
        fireFocusChangeEvent();

        -- 播放界面失去焦点的效果
        if table.indexOf(withOutFocusEffectForms, formGetFocus) <= 0 and
            table.indexOf(withOutFocusEffectForms, formLoseFocus) <= 0 then
            if formLoseFocusDialog then
                FormEffectM.runLoseFocusEffect(formLoseFocusDialog);
            end
        end
    end);

    -- 注册删除模态框的事件
    EventMgr.register("FormFocusM", event.REMOVE_DIALOG, function(params)
        if UIMgr.getCurrentScene() == nil then
            return;
        end

        formLoseFocus = formGetFocus;
        formLoseFocusDialog = formGetFocusDialog;

        local currentDialog = UIMgr.getCurrentScene():getCurrentDialog();
        if currentDialog then
            formGetFocus = currentDialog:getName();
            formGetFocusDialog = currentDialog;
        else
            formGetFocus = UIMgr.getCurrentScene():getCurrentForm():getName();
            formGetFocusDialog = UIMgr.getCurrentScene():getCurrentForm();
        end
        fireFocusChangeEvent();

        -- 播放界面获得焦点的效果
        if table.indexOf(withOutFocusEffectForms, formGetFocus) <= 0 and
            table.indexOf(withOutFocusEffectForms, formLoseFocus) <= 0 then
            if formGetFocusDialog then
                FormEffectM.runGetFocusEffect(formGetFocusDialog);
            end
        end
    end);
end

-- 发布焦点时间
function fireFocusChangeEvent()
    EventMgr.fire(event.FOCUS_CHANGED, { get = formGetFocus, lose = formLoseFocus });
    print("FOCUS CHANGED, get focus:" .. formGetFocus .. ", lose focus:" .. formLoseFocus);

    -- 特殊的窗口。这些窗口不需要去处理bg的显示和隐藏
    local specialFrame = {"UIHeroMain", "UILogin", "UIMarketMain", "UINewWorkshop",
       "UIWorkshopGuide", "UISkyCityRuins", "UIMercenaryCamp", "UITankUse",
       "UIPetRecruitMain", "UIPetRecruit", "UIFateChain"};

    -- 处理失去、获得焦点的窗口bg的显示和隐藏
    if table.indexOf(specialFrame, formGetFocus) < 0 then
        local uiForm = formGetFocusDialog;

        if not uiForm then
            return;
        end

        local node = uiForm.node;
        if not node then
            return;
        end

        local bgImg = node:getChildByName("bg");
        if bgImg then
            bgImg:setVisible(true);

            if formLoseFocusDialog ~= formGetFocusDialog and table.indexOf(specialFrame, formLoseFocus) < 0 then
                local uiForm = formLoseFocusDialog;
                if not uiForm then
                    return;
                end

                local node = uiForm.node;
                if not node then
                    return;
                end

                local bgImg = node:getChildByName("bg");
                if bgImg then
                    bgImg:setVisible(false);
                end
            end
        end
    end

    local function func()
        -- 告诉服务器，用于记录
        if AccountM.noticeFocusChange(focusList) then
            -- 清空
            focusList = {};
        end

        cc.Director:getInstance():getScheduler():unscheduleScriptEntry(schedulerId);
        schedulerId = nil;
    end

    -- 为了避免频繁发送，这里延时3秒再发送
    if #focusList <= 0 then
        schedulerId = cc.Director:getInstance():getScheduler():scheduleScriptFunc(func, 3, false);
        --performWithDelay(cc.Director:getInstance():getScheduler(), func, 3);
    end

    table.insert(focusList, { ["new"] = formGetFocus, ["old"] = formLoseFocus, });
end

-- 修改玩家累计无操作时间
function setUserIdleTime(setTime)
    userIdleTime = setTime;
end

-- 获取玩家累计无操作时间
function getUserIdleTime()
    return userIdleTime;
end
